<template>
  <el-row :gutter="20">
    <el-col :span="24">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="缩略图" prop="image" >
          <el-upload
            class="demoUploader"
            action="/api/user/upload"
            accept="image/png, image/jpeg,img/jpg,png,jpeg,jpg"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="ruleForm.image" :src="ruleForm.image" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="类别" prop="category">
          <el-select
            v-model="ruleForm.category"
            placeholder="类别"
            @change="changeType"
          >
            <el-option
              v-for="item in categoryOption"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="子类别" prop="kind">
          <el-select v-model="ruleForm.kind" placeholder="类别">
            <el-option
              v-for="item in kindOption"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="服务" prop="storeId">
          <el-select v-model="ruleForm.storeId" placeholder="服务">
            <el-option
              v-for="item in storeOption"
              :key="item.value"
              :label="item.name"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item> -->

        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio label="0">保存草稿</el-radio>
            <el-radio label="1">立即发布</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="简介">
          <el-input type="textarea" v-model="ruleForm.summary"></el-input>
        </el-form-item>
        <el-form-item label="正文">
          <vue-html5-editor
            class="zhengwen"
            :content="ruleForm.content"
            :height="400"
            @change="updateData"
          ></vue-html5-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">
            保存
          </el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
<script>
import { fieldMap, publicFilter } from "@/utils/showMap";
import store from "@/store";
import { getArticleInfo, articleInfo } from "@/api/service";
export default {
  data() {
    return {
      categoryOption: fieldMap.article_category,
      storeOption: [], // 服务中心
      kindOption: fieldMap.article_playSkill,
      ruleForm: {
        image: "",
        summary: "",
        id: "",
        title: "",
        content: "默认正文...",
        operatorId: "",
        category: "0",
        kind: "0",
        status: "0",
        storeId: null,
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        category: [
          {
            required: true,
            message: "请至少选择一种类别",
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    //加载
    if (this.$route.query.id) this.search(this.$route.query.id);
  },
  methods: {
    search(args) {
      getArticleInfo(args).then((res) => {
        for (let key in res.data) {
          if (this.ruleForm.hasOwnProperty(key))
            this.ruleForm[key] = String(res.data[key]);
        }
      });
    },
    changeType(args) {
      switch (args) {
        case "0":
          this.kindOption = fieldMap.article_playSkill;
          break;
        case "1":
          this.kindOption = fieldMap.article_blockchain;
          break;
        default:
          this.kindOption = [];
          break;
      }
    },
    updateData(args) {
      //富文本赋值
      this.ruleForm.content = args;
      // this.content = args;
      // let c1 = args.replace(/<img width="100%"/g, '<img');
      // let c2 = c1.replace(/<img/g, '<img width="100%"');
      // this.content = c2;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        // console.log(valid);
        if (valid) {
          articleInfo(this.ruleForm).then((res) => {
            this.$router.push("/manage/article");
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleAvatarSuccess(res, file) {
      console.log(res, file);
      this.ruleForm.image =res.data;// URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>
<style lang="less">
.zhengwen{
  img{
    max-width:100%;
  }
}
.demo-ruleForm {
  text-align: left;
  .demoUploader {
    .el-upload,.el-upload--text {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    .el-upload:hover {
      border-color: #409eff;
    }
    .avatar-uploader-icon {
      font-size: 28px; border: 1px dashed #d9d9d9;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
  }
}
</style>
